package com.hanthink.web.controller.app.equipment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.common.utils.ip.IpUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.equipment.EquipmentInfo;
import com.hanthink.system.domain.manhour.FactWorkerUsed;
import com.hanthink.system.domain.manhour.PLineEquipment;
import com.hanthink.system.domain.manhour.PLineMachineStructure;
import com.hanthink.system.domain.pc.EmEquip;
import com.hanthink.system.domain.repair.EquipOperLog;
import com.hanthink.system.mapper.equipment.EquipmentInfoMapper;
import com.hanthink.system.service.ISysDeptService;
import com.hanthink.system.service.equipment.IEquipmentInfoService;
import com.hanthink.system.service.manhour.IPLineEquipmentService;
import com.hanthink.system.service.manhour.IPLineMachineStructureService;
import com.hanthink.system.service.pc.IEmEquipService;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;

/**
 * @author: lizhao
 * @codefunction:
 * @creatdate: 2022-05-11 下午1:15
 */
@RestController
@RequestMapping("/app/threepiecesequipment")
public class ThreePiecesEquipmentManage extends BaseController {

    @Autowired
    private IEquipmentInfoService equipmentInfoService;

    @Autowired
    private IPLineEquipmentService ipLineEquipmentService;

    @Autowired
    private IPLineMachineStructureService ipLineMachineStructureService;

    @Autowired
    private IEmEquipService iEmEquipService;

    @Autowired
    private TokenService tokenService;


    /**
     * 根据公司代码查询产线列表
     * @param map
     * @return
     * @throws ParseException
     */
    @PostMapping("/querypline")
    public TableDataInfo querypline(@RequestBody Map<String, Object> map) throws ParseException{

        List<PLineEquipment> list = ipLineEquipmentService.selectPLineEquipmentListbyfactoryid(map.get("deptid").toString());
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }
    /**
     * 根据公司和产线代码查询设备明细
     * @param map
     * @return
     * @throws ParseException
     */
    @PostMapping("/info")
    public TableDataInfo list(@RequestBody Map<String, Object> map) throws ParseException {
        List<PLineMachineStructure> list = ipLineMachineStructureService.selectPLineMachineStructureListbyfactoryid(map.get("deptid").toString(),map.get("plineid").toString());

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    /**
     * 根据设备代码和产线名称查询设备清单
     * @param map 查询参数
     * @return 设备列表明细
     * @throws ParseException
     */
    @PostMapping("/infoname")
    public TableDataInfo infoname(@RequestBody Map<String, Object> map) throws ParseException {
        List<PLineMachineStructure> list = ipLineMachineStructureService.selectPLineMachineStructureListbyfactoryname(map.get("deptid").toString(),map.get("plinename").toString());

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    /**
     * 根据设备代码打开设备
     * @param map 设备代码
     * @return 操作状态
     * @throws ParseException
     */
    @PostMapping("/saveenable")
    private TableDataInfo saveopen(@RequestBody Map<String, Object> map) throws ParseException {
        TableDataInfo tableDataInfo = new TableDataInfo();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        EmEquip emEquip = new EmEquip();
        try {
            for (int i = 0; i < ((List<LinkedHashMap>)(map.get("result"))).size(); i++) {
                LinkedHashMap myArraylist = (LinkedHashMap) ((ArrayList) map.get("result")).get(i);
                emEquip.setSiteKey(myArraylist.get("factoryid").toString());
                Long id = Long.valueOf(String.valueOf(myArraylist.get("equipId")));
                emEquip.setId(id);
                emEquip.setName(myArraylist.get("machineid").toString());
                emEquip.setStatus("1");
                iEmEquipService.saveLog(emEquip,loginUser);
                iEmEquipService.updateEmEquipbymachineid(myArraylist.get("machineid").toString(),"1");
            }

           tableDataInfo.setCode(HttpStatus.SUCCESS);
           tableDataInfo.setMsg("操作成功");
           return tableDataInfo;
       }
       catch (Exception ex){
           tableDataInfo.setCode(HttpStatus.ERROR);
           tableDataInfo.setMsg("操作失败");
           return tableDataInfo;
       }
    }

    /**
     * 根据设备代码关闭设备
     * @param map 设备代码
     * @return 操作状态
     * @throws ParseException
     */
    @PostMapping("/savenoenable")
    private TableDataInfo saveclose(@RequestBody Map<String, Object> map) throws ParseException {
        TableDataInfo tableDataInfo = new TableDataInfo();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        EmEquip emEquip = new EmEquip();
        try {
            for (int i = 0; i < ((List<LinkedHashMap>)(map.get("result"))).size(); i++) {
                LinkedHashMap myArraylist = (LinkedHashMap) ((ArrayList) map.get("result")).get(i);
                emEquip.setSiteKey(myArraylist.get("factoryid").toString());
                Long id = Long.valueOf(String.valueOf(myArraylist.get("equipId")));
                emEquip.setId(id);
                emEquip.setName(myArraylist.get("machineid").toString());
                emEquip.setStatus("0");
                iEmEquipService.saveLog(emEquip,loginUser);
                iEmEquipService.updateEmEquipbymachineid(myArraylist.get("machineid").toString(),"0");
            }

            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            return tableDataInfo;
        }
        catch (Exception ex){
            tableDataInfo.setCode(HttpStatus.ERROR);
            tableDataInfo.setMsg("操作失败");
            return tableDataInfo;
        }
    }
}
